package com.onlinemathuracrud.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.onlinemathuracrud.dao.AccomodationDao;
import com.onlinemathuracrud.dao.CityDao;
import com.onlinemathuracrud.model.Accomodation;
import com.onlinemathuracrud.model.master.City;


@Controller
@RequestMapping(value = "/accomodation")
public class AccomodationCrudController {
	@Autowired
	AccomodationDao accomodationDao;
	@Autowired
	CityDao cityDao;

	@RequestMapping(value = "/showaccomodation", method = RequestMethod.GET)
	public String showcities(ModelMap model,@ModelAttribute("accomodationdata") Accomodation accomodation) {
		model.addAttribute("allAccomodations", accomodationDao.findAll());
		model.addAttribute("cities", cityDao.findAll());
		return "accomodations";
	}

	@RequestMapping(value = "/editaccomodation/{id}", method = RequestMethod.GET)
	public String editcity(ModelMap model, @PathVariable("id") Long accomodationId) {
		model.addAttribute("accomodationdata", accomodationDao.findById(accomodationId));
		model.addAttribute("allAccomodations", accomodationDao.findAll());
		model.addAttribute("cities", cityDao.findAll());
		return "accomodations";
	}

	@RequestMapping(value = "/deleteaccomodation/{id}", method = RequestMethod.GET)
	public String deletecity(ModelMap model,@PathVariable("id") Long accomodationId,
			@ModelAttribute("accomodationdata") Accomodation accomodation) {

		accomodationDao.delete(accomodationDao.findById(accomodationId));
		return "redirect:/accomodation/showaccomodation";
	}

	@RequestMapping(value = "/saveaccomodation", method = RequestMethod.POST)
	public String savecity(ModelMap model,Accomodation accomodation,HttpServletRequest request) {
		accomodation.setCity("dummy value for time being");
		accomodationDao.saveOrUpdate(accomodation);
		return "redirect:/accomodation/showaccomodation";
	}


}
